package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ioftv.entity.MapDistribution;
import com.ioftv.mapper.MapDistributionMapper;
import com.ioftv.service.MapDistributionService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;

/**
 * 地图分布数据Service实现
 */
@Service
public class MapDistributionServiceImpl extends BaseService<MapDistributionMapper, MapDistribution> implements MapDistributionService {

    @Override
    public List<MapDistribution> getByStatDate(LocalDate statDate, String province) {
        LambdaQueryWrapper<MapDistribution> wrapper = createUserQueryWrapper();
        wrapper.eq(MapDistribution::getStatDate, statDate);
        
        if (StringUtils.hasText(province)) {
            wrapper.eq(MapDistribution::getProvince, province);
        }
        
        wrapper.orderByDesc(MapDistribution::getBusinessCount);
        
        return this.list(wrapper);
    }
    
    @Override
    public List<MapDistribution> getByStatDateAndUserId(LocalDate statDate, String province, Long userId) {
        LambdaQueryWrapper<MapDistribution> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MapDistribution::getUserId, userId);
//        wrapper.eq(MapDistribution::getStatDate, statDate);
        
        if (StringUtils.hasText(province)) {
            wrapper.eq(MapDistribution::getProvince, province);
        }
        
        wrapper.orderByDesc(MapDistribution::getBusinessCount);
        
        return this.list(wrapper);
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<MapDistribution> queryWrapper) {
        queryWrapper.eq(MapDistribution::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(MapDistribution entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}